package cn.tedu.egaddress1013.controller;

import cn.tedu.egaddress1013.mapper.AddressMapper;
import cn.tedu.egaddress1013.pojo.dto.AddressAddParam;
import cn.tedu.egaddress1013.pojo.entity.Address;
import cn.tedu.egaddress1013.pojo.vo.AddressListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/address/")
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;

    @PostMapping("add")
    public String addAddress(@RequestBody AddressAddParam param){
        System.out.println("param:"+param);
        Address address=new Address();
        BeanUtils.copyProperties(param,address);
        addressMapper.insertAddress(address);
        return "添加完成";
    }
    @GetMapping("list")
    public List<AddressListVO> addressList(){
        return addressMapper.addresslistvos();
    }

    @PostMapping("del/{id}")
    public String delAddress(@PathVariable int id){
        System.out.println("id："+id);
        int rows=addressMapper.deleteAddressById(id);
        if (rows>0){
            return "删除完成";
        }else{
        return "数据已删除，请重试";}
    }

}
